package org.zhouhe.util;

import java.util.UUID;

public class Utils {
	private static final int defaultLength = 20;

	public static String title(String title) {
		return formatTitle(title, defaultLength);
	}

	public static String formatTitle(String title, int n) {
		boolean cuted = false;
		int total = 0;
		StringBuilder sb = new StringBuilder();
		if (title != null) {
			title = title.trim();
			char[] titleArr = title.toCharArray();
			for (char c : titleArr) {
				int plus = 1;
				int len = (int) c;
				if (len > 127) {
					plus++;
				}
				if (total + plus <= n) {
					sb.append(c);
					total += plus;
				} else {
					cuted = true;
					break;
				}
			}
			if (cuted) {
				sb.append("...");
			}
		}
		return sb.toString();
	}

	public static String generateGUID() {
		UUID uuid = UUID.randomUUID();
		return uuid.toString().replaceAll("-", "");
	}

	public static void main(String[] args) {
		System.out.println(formatTitle("周鹤zhouhe", 6));
	}

}
